// pages/lawyerConfirm/lawyerConfirm.js
const app = getApp();
import cityData from '../../utils/vendor/cityList.js';
import util from '../../utils/util.js';
import request from '../../utils/config.js';
let provinceList = new Array();
let cityListTemp = new Array();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userExt:{},
    provinceList:[],
    cityList:[],
    provinceIndex: 'none',
    cityIndex: 'none',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.userExt.mobileNo){
      this.setData({ 
        userExt: app.globalData.userExt,
        userName: app.globalData.userExt.userName,
        lawfirmName: app.globalData.userExt.lawfirmName,
        lawyerNo: app.globalData.userExt.lawyerNo,
      })
    }else{
      this.getUserExt()
    }
    this.collateData();
    this.defaultArea();
  },
  collateData:function () {
    cityListTemp = new Array();
    for (let key in cityData){
      provinceList.push(key);
    }
    if (app.globalData.userExt.province) {
      this.checkCity(app.globalData.userExt.province);
    }
    this.setData({ provinceList: provinceList });
  },
  checkCity:function (province) {
    let cityList = new Array();
    let provinceName = province||'';
    for (let key in cityData[provinceName]){
      cityList.push(key)
    }
    this.setData({ cityList: cityList })
  },
  defaultArea: function () {
    if (app.globalData.userExt.province) {
      for (let i = 0; i < this.data.provinceList.length; i++) {
        if (app.globalData.userExt.province == this.data.provinceList[i]) {
          this.setData({ provinceIndex: i })
          break;
        }
      }
    }
    if (app.globalData.userExt.city) {
      for (let i = 0; i < this.data.cityList.length; i++) {
        if (app.globalData.userExt.city == this.data.cityList[i]) {
          this.setData({ cityIndex: i })
          break;
        }
      }
    }
  },
  //获取扩展信息
  getUserExt: function () {
    //获取第三方接口登录数据
    let headerInfo = {
      sessionId: app.globalData.sessionId,
      pageNum: '',
      pageSize: '',
    }, _this = this;
    console.log('000')
    request.requestLogin_api("queryUserExt", headerInfo, {}, (res) => {
      let Res = JSON.parse(res.data.body)
      _this.setData({
        userExt: Res,
        userName: Res.userName,
        lawfirmName: Res.lawfirmName,
        lawyerNo: Res.lawyerNo,
      });
      _this.defaultArea();
      app.globalData.userExt = Res;
      console.log("获取扩展信息", Res);
    }, (res) => {
    });
  },

  submit:function(){
    if (!this.data.userName){
      wx.showToast({title:'律师姓名不能为空',icon:'none'})
      return;
    }
    if (!this.data.lawfirmName) {
      wx.showToast({ title: '律所名称不能为空', icon: 'none' })
      return;
    }
    if (!this.data.lawyerNo) {
      wx.showToast({ title: '执业证号不能为空', icon: 'none' })
      return;
    }
    if (!util._YHC_util.isLawyerNo(this.data.lawyerNo)) {
      wx.showToast({ title: '执业证号有误', icon: 'none' })
      return;
    }
    if (this.data.provinceIndex=='none') {
      wx.showToast({ title: '省份不能为空', icon: 'none' })
      return;
    }
    if (this.data.cityIndex=='none') {
      wx.showToast({ title: '城市不能为空', icon: 'none' })
      return;
    }
    let send = {
      userName: this.data.userName,
      lawfirmName: this.data.lawfirmName,
      lawyerNo: this.data.lawyerNo,
      province: this.data.provinceList[this.data.provinceIndex],
      city: this.data.cityList[this.data.cityIndex],
      lawyerYear: this.data.lawyerNo.substr(5, 4)
    };
    console.log(send)
    let _this = this;
    wx.showLoading({ title: ' ', mask: true })
    request.requestLogin_api('userExt4Lawyer', {
      sessionId: app.globalData.sessionId,
      pageNum: '',
      pageSize: '',
    }, send, (res) => {
      console.log("userExt4Lawyer", res)
      wx.hideLoading()
      if (res.data.isSuccess == 'T') {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function () {
            wx.navigateBack();
          }, 1000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    }, (res) => {
      console.log("提交失败", res)
      wx.showToast({
        title: '提交失败',
        icon: 'none',
        duration: 1000
      })
    });
  },
  change_userName: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
  change_lawfirmName: function (e) {
    this.setData({
      lawfirmName: e.detail.value
    })
  },
  change_lawyerNo: function (e) {
    this.setData({
      lawyerNo: e.detail.value
    })
  },
  pickerChange_province: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    if (e.detail.value != this.data.provinceIndex) {
      this.setData({
        provinceIndex: e.detail.value,
        cityIndex:'none'
      })
      this.checkCity(this.data.provinceList[e.detail.value])
    }
  },
  pickerChange_city: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      cityIndex: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})